//* Copyright(C) 2005-2012, all rights reserved.
//*
//* The following code is copyrighted and contains proprietary information
/* and trade secrets of Foxit Software Inc.      
**************************************************************************
*/
/**
* 工具栏插件类；
*/
define(function (require, exports, module) {
	WebPDF.RIBBON_STATUSBAR_PLUGIN_NAME = "StatusBar Ribbon Plugin";
    /**
     * Plugin of ribbon status bar.
     * @param {WebPDF.ReaderApp} readerApp app instance
     * @extends {WebPDF.IReaderPlugin}
     * @constructor
     */
    WebPDF.RibbonStatusBarPlugin = function (readerApp) {
        var _readerApp = readerApp,
            _isInitialized = false;

        var _ribbonDivID = _readerApp.getBottomPanelID()+"_ribbon";
        var _bottomBarDivID = _ribbonDivID + "_bottombar";
        var _centerBarDivID = _bottomBarDivID + "_center";
        var _rightBarDivID = _bottomBarDivID+ "_righttools";

        //获取Plugin名称;
        this.getPluginName = function () {
            return WebPDF.RIBBON_STATUSBAR_PLUGIN_NAME;
        };

        //插件被注册的时候调用;
        this.onRegister = function () {
            if (!WebPDF.Config.ToolBar)
                return false;
            if (_isInitialized)
                return true;
            var jqBottomPanel = $("#" + _readerApp.getBottomPanelID());
            var ribbonPanelDiv = "<div id='"+_ribbonDivID+"' class='fwr_ribbon_bottom_panel' />";
            jqBottomPanel.append(ribbonPanelDiv);
            var jqRibbonDiv = $("#"+_ribbonDivID);
            var bottomDiv = "<div id='" + _bottomBarDivID + "' class='fwr_ribbon_bottombar'/>";
            jqRibbonDiv.append(bottomDiv);
           
            var jqBottomBar = $("#"+_bottomBarDivID);
            var centerDiv = "<span id='" + _centerBarDivID + "' class='fwr_ribbon_bottombar_rowcenter' />";
            jqBottomBar.append(centerDiv);
            
            var rightToolBarDiv = "<span class='fwr_ribbon_bottombar_rowright' id='" + _rightBarDivID + "' />";
            jqBottomBar.append(rightToolBarDiv);
            
            jqBottomPanel.show();
        };

        //模块初始化;
        this.init = function () {
            if (!WebPDF.Config.ToolBar)
                return false;
            if (_isInitialized)
                return true;
            _createToolBarHtml(false,_centerBarDivID);
            _createToolBarHtml(true,_rightBarDivID);
            _initToolBarUI(true);
            _initToolBarUI(false);
            _isInitialized = true;
        };

        function _createToolBarHtml(isRight, parentDivID) {
            var RibbonStatusBar = WebPDF.Config.RibbonStatusBar;
        	var tools = isRight ? RibbonStatusBar.right : RibbonStatusBar.center;
        	var toolsNum = tools.length;
        	var toolbarHtml = "";
        	for(var i = 0; i < toolsNum; i++)
        	{
        		 var btnName = tools[i];
                 var commandUI = _readerApp.getCommandUI(btnName);
                 if (!commandUI)
                     continue;
                 toolbarHtml += commandUI.createHtml();
        	}
        	 var jqToolBar = $("#"+parentDivID);
        	 jqToolBar.append(toolbarHtml);
        }
        
        function _initToolBarUI(isRight) {
            var RibbonStatusBar = WebPDF.Config.RibbonStatusBar;
            var tools = isRight ? RibbonStatusBar.right : RibbonStatusBar.center;
        	var toolsNum = tools.length;
        	for(var i = 0; i < toolsNum; i++)
        	{
        		 var btnName = tools[i];
                 var commandUI = _readerApp.getCommandUI(btnName);
                 if (!commandUI)
                     continue;
                commandUI.init(_readerApp);
        	}
        }

        //模块卸载;
        this.unLoad = function () {
        };

        this.getReaderApp = function () {
            return _readerApp;
        };
    };

    exports.createPlugin = function (readerApp) {
        return new WebPDF.RibbonStatusBarPlugin(readerApp);
    };
});